package com.seisys.service.system.office;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.seisys.dao.DaoSupport;
import com.seisys.entity.Page;
import com.seisys.entity.system.Area;
import com.seisys.entity.system.Office;
import com.seisys.util.PageData;

@Service("officeService")
public class OfficeService {

	@Resource(name = "daoSupport")
	private DaoSupport dao;

	/*
	 * 新增
	 */
	public void save(PageData pd) throws Exception {
		dao.save("OfficeMapper.save", pd);
	}

	/*
	 * 删除
	 */
	public void delete(PageData pd) throws Exception {
		dao.delete("OfficeMapper.delete", pd);
	}

	/*
	 * 修改
	 */
	public void edit(PageData pd) throws Exception {
		dao.update("OfficeMapper.edit", pd);
	}

	/*
	 * 列表
	 */
	public List<PageData> list(Page page) throws Exception {
		return (List<PageData>) dao.findForList("OfficeMapper.datalistPage",
				page);
	}

	/*
	 * 列表(全部)
	 */
	public List<PageData> listAll(PageData pd) throws Exception {
		return (List<PageData>) dao.findForList("OfficeMapper.listAll", pd);
	}

	/*
	 * 查找所有父类
	 */
	public List<Office> listAllParentOffice() throws Exception {
		return (List<Office>) dao.findForList(
				"OfficeMapper.listAllParentOffice", null);

	}

	/*
	 * 根据父类ID找子节点
	 */
	public List<Office> listSubOfficeByParentId(String parentId)
			throws Exception {
		return (List<Office>) dao.findForList(
				"OfficeMapper.listSubOfficeByParentId", parentId);

	}

	public List<Office> listSubOfficeById(String officeId) throws Exception {
		return (List<Office>) dao.findForList("OfficeMapper.listSubOfficeById",
				officeId);

	}

	public Office getOfficeById(String officeId) throws Exception {
		return (Office) dao.findForObject("OfficeMapper.getOfficeById",
				officeId);

	}

	public List<Office> listOffice(Map<String, Object> map) throws Exception {
		return (List<Office>) dao.findForList("OfficeMapper.OfficeList", map);

	}

	public java.lang.Integer findOfficeCont() throws Exception {
		return (java.lang.Integer) dao.findForObject(
				"OfficeMapper.findOfficeCont", "");
	}

	public List<Office> findAllOffice() throws Exception {
		return (List<Office>) dao.findForList("OfficeMapper.findAllOffice", "");
	}

	public List<Office> searchList(Map<String, Object> map) throws Exception {
		return (List<Office>) dao.findForList("OfficeMapper.searchList", map);

	}

	public java.lang.Integer searchListCont(Map<String, Object> map)
			throws Exception {
		return (java.lang.Integer) dao.findForObject(
				"OfficeMapper.searchListCont", map);

	}

	public List<Area> getAreaList() throws Exception {
		return (List<Area>) dao.findForList("AreaMapper.listAllOffice", "");

	}

	/*
	 * 表格数据
	 */
	public List<Office> getOfficeList(Map<String, Object> map) throws Exception {
		List<Office> rusl = new ArrayList<Office>();
		rusl = this.listOffice(map);
		return rusl;
	}

	/*
	 * 通过点击树获得改组织所有
	 */
	public List<Office> listClickAllOffice(String officeId) throws Exception {
		List<Office> rusl = new ArrayList<Office>();
		Office of = this.getOfficeById(officeId);
		if (of != null) {
			rusl.add(of);// 点击的本身的对象
		}
		List<Office> rl = this.listSubOfficeByParentId(officeId);// 第一波子类
		if (rl.size() != 0) {
			for (int i = 0; i < rl.size(); i++) {
				rusl.add(rl.get(i));// 每循环一次把子类加进去
				List<Office> subList = this.listSubOfficeByParentId(rl.get(i)
						.getOffice_id());// 查找该节点的子节点
				if (subList.size() != 0) {
					for (Office office : subList) {
						rusl.add(office);
					}
					rusl = this.listClickAllSubOffice(subList, rusl);
				}
			}
		}
		return rusl;
	}

	public List<Office> listClickAllSubOffice(List<Office> subList,
			List<Office> rusl) throws Exception {

		if (subList.size() != 0) {
			for (int i = 0; i < subList.size(); i++) {
				List<Office> subList2 = this.listSubOfficeByParentId(subList
						.get(i).getOffice_id());// 查找该节点的子节点
				if (subList2.size() != 0) {
					for (Office office : subList2) {
						rusl.add(office);
					}
					rusl = this.listClickAllSubOffice(subList2, rusl);
				}
			}
		}

		return rusl;

	}

	public List<Office> listAllOffice() throws Exception {
		List<Office> rl = this.listAllParentOffice();
		for (Office office : rl) {
			List<Office> subList = this.listSubOfficeByParentId(office
					.getOffice_id());// 根据parentid==0的节点的id查找子节点
			if (subList.size() != 0) {
				subList = this.findSubOffice(subList);
			}
			office.setSubOffice(subList);
		}
		return rl;

	}

	public List<Office> findSubOffice(List<Office> of) throws Exception {
		for (Office triOffice : of) {
			List<Office> triOfficeList = new ArrayList<Office>();
			// String[] ary = triMenu.getPARENT_IDS().split(",");
			String triOfficeID = triOffice.getOffice_id();
			triOfficeList = this.listSubOfficeById(triOfficeID);
			if (triOfficeList.size() != 0) {
				triOfficeList = this.findSubOffice(triOfficeList);
				triOffice.setSubOffice(triOfficeList);
			}
		}
		return of;
	}

	/*
	 * 通过id获取数据
	 */
	public PageData findById(PageData pd) throws Exception {
		return (PageData) dao.findForObject("OfficeMapper.findById", pd);
	}

	/*
	 * 批量删除
	 */
	public void deleteAll(String[] ArrayDATA_IDS) throws Exception {
		dao.delete("OfficeMapper.deleteAll", ArrayDATA_IDS);
	}

	public List<Office> getListOffice(Page pg) throws Exception {
		return (List<Office>) dao.findForList("OfficeMapper.getListOffice", pg);
	}

	public List<Office> getSearchList(Page pg) throws Exception {
		return (List<Office>) dao.findForList("OfficeMapper.getSearchList", pg);
	}

}
